<?php

namespace app\beautiful\controller\admin\member;

use app\BaseController;

use app\beautiful\logic\PartnerLogic;
use app\common\logic\DataLogic;

use app\beautiful\logic\LogsLogic;

use app\beautiful\logic\MemberWalletLogic;

use app\beautiful\model\logs\MemberSupplementModel;

use app\beautiful\model\member\MemberModel;

use think\facade\Db;



class Supplement extends BaseController{

    public function index()

    {

        $nick_name=$this->request->param('nick_name');

        $mobile=$this->request->param('mobile');

        $from_type = $this->request->param('from_type/d',1);

        $where=[

            ['shop_id','=',$this->request->shopId],

        ];
        $member_where['is_delete'] = 0;
        if($from_type==1) $member_where['shop_id'] = $this->request->shopId;

        if(!empty($nick_name)) $member_where[]=['nick_name','=',$nick_name];

        if(!empty($mobile)) $member_where[]=['mobile','=',$mobile];

        $member_ids=MemberModel::where($member_where)->column('member_id') ?? [];

        $where[]=['member_id','in',$member_ids];

        $shop_id = $this->request->shopId;
        $supplement_model= new MemberSupplementModel();
        $supplement_model=$supplement_model->with(['member'=>function($query) use ($shop_id){
            $query->with(['partner'=>function($query1) use ($shop_id){
                $query1->where(['shop_id'=>$shop_id]);
            }]);
        }]);

        $order=['add_time'=>'desc'];

        $field='*';

        $append=['add_time_format'];



        $return = DataLogic::getDataList($supplement_model,$where,$order,$field,$this->request->limit,$this->request->page,$append);

        $list = $return['list'];

        foreach($list as $k=>$v){
            //判断会员是否是股东
            $list[$k]['member']['name']=$v['member']['partner']?$v['member']['partner']['name']:null;
            unset($list[$k]['member']['partner']);
        }

        $return['list'] = $list;

        return $this->success($return);

    }



    public function getMemberWithMobileFromSupplement()

    {

        $mobile = $this->request->param('mobile');

        if(empty($mobile) || !is_mobile($mobile)){

            return $this->error('请输入手机号');

        }



        if (null === $member = MemberModel::where([

                ['mobile','=',$mobile],

                ['shop_id','=',$this->request->shopId],

                ['status','=',1],

                ['is_delete','=',0]

            ])->find()) {

            return $this->error('会员不存在');

        }

        $member->toArray();

        return $this->success(['member'=>$member]);

    }



    public function save()

    {

        $member_id = $this->request->param('member_id/d',0);

        $money=$this->request->param('money/f',0);

        $gross=$this->request->param('gross/f',0);

        if($member_id==0) return $this->error('请选择会员');

        if($money<=0) return $this->error('请输入金额，且金额不能小于等于0');
        if($gross<0 || $gross>$money) return $this->error('请输入毛利，且金额不能小于0，不能大于结算金额');



        if(null === $member = MemberModel::find($member_id)) return $this->error('会员不存在');

        if($member->shop_id != $this->request->shopId) return $this->error('会员不存在');

        if($member->is_delete != 0 ) return $this->error('会员不存在');



        Db::startTrans();

        try{

            //先充值

            $MemberWalletLogic = new MemberWalletLogic($this->request->shopId, $member_id);

            $res = $MemberWalletLogic->addBalance($money, 0, 10,'', $this->request->accountId);

            if($res == false){

                Db::rollback();

                return $this->error($MemberWalletLogic->getError());

            }



            //在消耗

            $res = $MemberWalletLogic->useBalance($money, $money, 11,'', $this->request->accountId);

            if($res == false){

                Db::rollback();

                return $this->error($MemberWalletLogic->getError());

            }



            //插入一条记录

            $res=LogsLogic::supplement($this->request->shopId,$member->member_id,2,$money,$this->request->accountId);

            if(!$res){

                Db::rollback();

                return $this->error('操作失败');

            }

            //记录推广员业绩
            $PartnerLogic = new PartnerLogic($this->request->shopId);
            $PartnerLogic->commissionNew($member_id,$money,'supplement',$gross);

            Db::commit();

            return $this->success('补单成功');





        }catch (\Exception $e){

            Db::rollback();

            return $this->error($e->getMessage());

        }

    }

}

